package com.siwei.mes.controller;


import com.siwei.mes.common.annotation.AnonymousAccess;
import com.siwei.mes.common.result.ApiResult;
import com.siwei.mes.common.result.Json;
import com.siwei.mes.common.util.DateHelper;
import com.siwei.mes.config.UidUtils;
import com.siwei.mes.entity.experiment.Experiment;
import com.siwei.mes.entity.order.Order;
import com.siwei.mes.entity.sync.tc.Waybill;
import com.siwei.mes.enums.GenerateCodeEnum;
import com.siwei.mes.handler.ExperimentFactory;
import com.siwei.mes.handler.service.ExperimentFactoryService;
import com.siwei.mes.job.*;
import com.siwei.mes.mapper.syncTc.WaybillMapper;
import com.siwei.mes.service.experiment.ExperimentSampleTakeService;
import com.siwei.mes.service.order.OrderService;
import com.siwei.mes.sync.config.SyncProcessorService;
import com.siwei.mes.sync.erp.entity.ItemorderFinish;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 测试接口
 *
 * @Description: 测试接口
 * @Version: 1.0
 * @author: chenmaolin
 * @date: 2024/1/6 22:22
 */

@Api(value = "测试接口")
@RestController
@RequestMapping("test")
public class TestController {
    @Resource
    private CleanSerialJob cleanSerialJob;
    @Resource
    private ExperimentFactory experimentFactory;
    @Resource
    private ExperimentStatusJob experimentStatusJob;
    @Resource
    private OrderService orderService;
    @Resource
    private RawMaterialsWeekJob materialsWeekJob;
    @Resource
    private ClearConcreteTimeJob clearConcreteTimeJob;
    @Resource
    private CleanConcreteYearConfigJob cleanConcreteYearConfigJob;
    @Resource
    private ExperimentSampleTakeService experimentSampleTakeService;
    @Resource
    private SyncProcessorService syncProcessorService;
    @Resource
    private WaybillMapper waybillMapper;
    @Resource
    private CleanExperimentCheckWaybillWeekJob checkWaybillWeekJob;
    @Resource
    private ClearMaterialsTimeJob clearMaterialsTimeJob;

    @AnonymousAccess
    @GetMapping("testGen")
    public Json testGen() {
        String s = GenerateCodeEnum.KLLZ_REPORTID.getCode() + DateHelper.getYear() + UidUtils.genNoNumber(GenerateCodeEnum.KLLZ_REPORTID.getCode(), "%05d");
        return Json.succ().data(s);
    }

    @AnonymousAccess
    @GetMapping("clearMaterialsTimeJob")
    public Json clearMaterialsTimeJob() {
        clearMaterialsTimeJob.clearJob();
        return Json.succ();
    }

    @AnonymousAccess
    @GetMapping("checkWaybillWeekInfo")
    public Json checkWaybillWeekInfo() {
        checkWaybillWeekJob.cleanCheckWaybillWeekInfo();
        return Json.succ();
    }


    @AnonymousAccess
    @GetMapping("cleanConcreteYearConfigJob")
    public Json cleanConcreteYearConfigJob() {
        cleanConcreteYearConfigJob.clearInfo();
        return Json.succ();
    }

    @AnonymousAccess
    @GetMapping("testClearConcreteTimeJob")
    public Json testClearConcreteTimeJob() {
        clearConcreteTimeJob.clearJob();
        return Json.succ();
    }

    /**
     * 56980,56983,56985,56988,56994,57000
     *
     * @return
     */
    @AnonymousAccess
    @GetMapping("testGenConcrete")
    public Json testGenConcrete() {
        Integer frwd = 77176;
        List<Integer> itemorderIdList = new ArrayList<>();
        itemorderIdList.add(3);
        for (Integer itemorderId : itemorderIdList) {
            ItemorderFinish itemorderFinish = new ItemorderFinish();
            itemorderFinish.setITID(itemorderId);
            itemorderFinish.setFRwdh(frwd);
            itemorderFinish.setFinishCarNumber(2);
            syncProcessorService.submitRequest(itemorderFinish);
        }
        return Json.succ();
    }

    @AnonymousAccess
    @GetMapping("testGenWaybill")
    public Json testGenWaybill(@RequestParam(required = false, name = "str") String str) {
        if (StringUtils.isBlank(str)) {
            str = "1,2,3,4,5,6,7,8";
        }
        String[] waybillIdList = str.split(",");
        for (String waybillId : waybillIdList) {
            Waybill waybill = waybillMapper.selectByWid(Long.valueOf(waybillId));
            syncProcessorService.submitRequest(waybill);
        }
        return Json.succ();
    }

    @AnonymousAccess
    @GetMapping("testFactory")
    public Json testFactory(@RequestParam("type") String type) {
        ExperimentFactoryService instance = experimentFactory.getInstance(type);
        instance.checkIsBatchFinish(new Experiment());
        return Json.succ();
    }

    @AnonymousAccess
    @ApiModelProperty(value = "清空序号表(按天)")
    @GetMapping("cleanSerial")
    public Json cleanSerialDay() {
        cleanSerialJob.init();
        return Json.succ();
    }

    @AnonymousAccess
    @ApiModelProperty(value = "清空序号表(按年)")
    @GetMapping("cleanSerialYear")
    public Json cleanSerialYear() {
        cleanSerialJob.initYear();
        return Json.succ();
    }

    @AnonymousAccess
    @ApiModelProperty(value = "测试生产编号")
    @GetMapping("genCode")
    public Json genCode(@RequestParam("code") String code, @RequestParam(required = false, name = "size") Integer size) {
        List<String> codeList = new ArrayList<>();
        if (size == null) {
            String s = UidUtils.genNo(code);
            return Json.succ().data(ApiResult.addColumn("code", s));
        } else {
            for (int i = 0; i < size; i++) {
                codeList.add(UidUtils.genNo(code));
            }
            return Json.succ().data(ApiResult.addList(codeList));
        }
    }


    @AnonymousAccess
    @ApiModelProperty(value = "测试修改实验委托状态")
    @GetMapping("checkExperimentStatus")
    public Json checkExperimentStatus() {
        experimentStatusJob.init();
        return Json.succ();
    }


    @AnonymousAccess
    @ApiModelProperty(value = "测试工单生成试验台账")
    @GetMapping("genOrder")
    public Json genOrder(@RequestParam("orderId") Long orderId) {
        Order order = orderService.loadById(orderId);
        if (order != null) {
            order.setGenQuick(1);
            orderService.finish(order);
        }
        return Json.succ();
    }

    @AnonymousAccess
    @ApiModelProperty(value = "根据星期定时清空原材料相关")
    @GetMapping("checkMaterialsWeek")
    public Json checkMaterialsWeek() {
        materialsWeekJob.init();
        return Json.succ();
    }

    @AnonymousAccess
    @ApiModelProperty(value = "根据检测类型生成原材料实验台账")
    @GetMapping("rawMaterialsExperimentNew")
    public Json rawMaterialsExperimentNew() {
        /*rawMaterialsExperimentNewJob.init();*/
        return Json.succ();
    }

    @AnonymousAccess
    @ApiModelProperty(value = "混凝土取样")
    @PostMapping("concreteSample")
    public Json concreteSample(@RequestParam("experimentId") Long experimentId) {
        experimentSampleTakeService.concreteSample(experimentId);
        return Json.succ();
    }
}
